<template>
	<view class="cart-container" v-if="cartStore.cartList.length">
		<!-- 收获地址组件 -->
		<my-address></my-address>
		<!-- 购物车商品列表的标题区域 -->
		<view class="cart-title">
			<uni-icons type="shop" size="18"></uni-icons>
			<text class="cart-title-text">购物车</text>
		</view>
		<uni-swipe-action>
			<view v-for="(item,index) in cartStore.cartList" :key="item.goods_id">
				<uni-swipe-action-item  :right-options="options" @click="swipeActionClickHandler(item)">
					<my-goods :goods="item" :show-radio="true" :show-num="true" @radiochange="radioChangeHandler"
						@num-change="numberChangeHandler">
					</my-goods>
				</uni-swipe-action-item>
			</view>
		</uni-swipe-action>
		<my-settle></my-settle>
	</view>
	<view class="empty-cart" v-else>
		<image src="../../static/cart_empty@2x.png" mode="aspectFill" class="empty-img"></image>
		<text class="tip-text">空空如也~</text>
	</view>
</template>

<script setup>
	import {onShow} from '@dcloudio/uni-app'
	import { computed, ref,watch } from 'vue';
	import { cartMixin } from '@/mixins/tabbar-badge.js';
	const  {cartStore,setBadge } = cartMixin.setup();
	const options = ref([
		{
			text:'删除',
			style:{
				backgroundColor: '#C00000' // 按钮的背景颜色
			}
		}
	])
	const total = computed(()=>{
		return cartStore.total
	})
	watch(total,()=>{
		setBadge();
	})
	const radioChangeHandler = (e) => {
		cartStore.updateGoodsState(e)
	}
	const numberChangeHandler = (e) => {
		cartStore.updateGoodsCount(e)
	}
	const swipeActionClickHandler = (item) => {
		cartStore.removeGoodsById(item.goods_id)
	}
	const gotoDetail = (item) => {
		// event.preventDefault()
		uni.navigateTo({
			url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
		})
	}
</script>

<style lang="scss" scoped>
	.cart-container{
		padding-bottom: 50px;
	}
	.cart-title {
		height: 40px;
		display: flex;
		align-items: center;
		font-size: 14px;
		padding-left: 5px;
		border-bottom: 1px solid #efefef;

		.cart-title-text {
			margin-left: 10px;
		}
	}
	.empty-cart{
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 150px;
		.empty-img{
			width: 90px;
			height: 90px;
		}
		.tip-text{
			font-size: 12px;
			color: gray;
			margin-top: 15px;
		}
	}
</style>